/** 隐形车辆分析器 */
class ShadowVehicleAnalyzer extends BaseAnalyzer {
    constructor(caseId, targetSubject = null) {
        super(caseId, targetSubject);
        this.name = '隐形车辆';
    }
    async analyze(params = {}) {
        const sql = `SELECT vehicle_number, driver_name, COUNT(*) as trip_count FROM tolling_records WHERE case_id = ? GROUP BY vehicle_number HAVING trip_count >= 5 ORDER BY trip_count DESC LIMIT 30`;
        const result = await this.runQuery(sql, [this.caseId]);
        return this.createResult('shadow_vehicle', this.name, result.data || [], `发现 ${result.data?.length || 0} 个活跃车辆`, 'info');
    }
    renderResult(data) {
        if (!data || data.length === 0) return '<div class="empty-result" style="padding:40px; text-align:center; color:#999;">✓ 无数据</div>';
        return `<table style="width:100%; border-collapse:collapse;"><thead><tr><th style="padding:10px; border:1px solid #e0e0e0;">车牌</th><th style="padding:10px; border:1px solid #e0e0e0;">驾驶人</th><th style="padding:10px; border:1px solid #e0e0e0;">次数</th></tr></thead><tbody>${data.map(r => `<tr><td style="padding:8px; border:1px solid #e0e0e0;">${r.vehicle_number}</td><td style="padding:8px; border:1px solid #e0e0e0;">${r.driver_name || '-'}</td><td style="padding:8px; border:1px solid #e0e0e0;">${r.trip_count}</td></tr>`).join('')}</tbody></table>`;
    }
}
window.ShadowVehicleAnalyzer = ShadowVehicleAnalyzer;

/** 影子股东分析器 */
class ShadowShareholderAnalyzer extends BaseAnalyzer {
    constructor(caseId, targetSubject = null) {
        super(caseId, targetSubject);
        this.name = '影子股东';
    }
    async analyze(params = {}) {
        const sql = `SELECT shareholder_name, company_name, shareholding_ratio FROM shareholder_records WHERE case_id = ? ORDER BY shareholding_ratio DESC LIMIT 50`;
        const result = await this.runQuery(sql, [this.caseId]);
        return this.createResult('shadow_shareholder', this.name, result.data || [], `发现 ${result.data?.length || 0} 条股权记录`, 'info');
    }
    renderResult(data) {
        if (!data || data.length === 0) return '<div class="empty-result" style="padding:40px; text-align:center; color:#999;">✓ 无数据</div>';
        return `<table style="width:100%; border-collapse:collapse;"><thead><tr><th style="padding:10px; border:1px solid #e0e0e0;">股东</th><th style="padding:10px; border:1px solid #e0e0e0;">公司</th><th style="padding:10px; border:1px solid #e0e0e0;">持股比例</th></tr></thead><tbody>${data.map(r => `<tr><td style="padding:8px; border:1px solid #e0e0e0;">${r.shareholder_name}</td><td style="padding:8px; border:1px solid #e0e0e0;">${r.company_name}</td><td style="padding:8px; border:1px solid #e0e0e0;">${r.shareholding_ratio}%</td></tr>`).join('')}</tbody></table>`;
    }
}
window.ShadowShareholderAnalyzer = ShadowShareholderAnalyzer;

/** 家庭资产穿透分析器 */
class FamilyWealthAnalyzer extends BaseAnalyzer {
    constructor(caseId, targetSubject = null) {
        super(caseId, targetSubject);
        this.name = '家庭资产穿透';
    }
    async analyze(params = {}) {
        // 简化版：统计关联人员的资产
        const sql = `SELECT customer_name, COUNT(DISTINCT account_card) as account_count FROM bank_transactions WHERE case_id = ? GROUP BY customer_name ORDER BY account_count DESC LIMIT 20`;
        const result = await this.runQuery(sql, [this.caseId]);
        return this.createResult('family_wealth', this.name, result.data || [], `统计 ${result.data?.length || 0} 个人员资产`, 'info');
    }
    renderResult(data) {
        if (!data || data.length === 0) return '<div class="empty-result" style="padding:40px; text-align:center; color:#999;">✓ 无数据</div>';
        return `<table style="width:100%; border-collapse:collapse;"><thead><tr><th style="padding:10px; border:1px solid #e0e0e0;">姓名</th><th style="padding:10px; border:1px solid #e0e0e0;">银行账户数</th></tr></thead><tbody>${data.map(r => `<tr><td style="padding:8px; border:1px solid #e0e0e0;">${r.customer_name}</td><td style="padding:8px; border:1px solid #e0e0e0;">${r.account_count}</td></tr>`).join('')}</tbody></table>`;
    }
}
window.FamilyWealthAnalyzer = FamilyWealthAnalyzer;

/** 综合画像分析器 */
class ComprehensiveProfileAnalyzer extends BaseAnalyzer {
    constructor(caseId, targetSubject = null) {
        super(caseId, targetSubject);
        this.name = '综合画像';
    }
    async analyze(params = {}) {
        if (!this.targetSubject) {
            return this.createResult('profile', this.name, [], '请选择分析对象', 'info');
        }
        const profile = {
            name: this.targetSubject.name,
            idCard: this.targetSubject.idCard,
            bankAccounts: this.targetSubject.accounts.bank.length,
            phones: this.targetSubject.accounts.phone.length
        };
        return this.createResult('profile', this.name, [profile], `${profile.name} 的综合画像`, 'info');
    }
    renderResult(data) {
        if (!data || data.length === 0) return '<div class="empty-result" style="padding:40px; text-align:center; color:#999;">请选择分析对象</div>';
        const profile = data[0];
        return `<div style="padding:20px; background:#f0f9ff; border-radius:8px;"><h4>${profile.name}</h4><p>身份证: ${profile.idCard || '未知'}</p><p>🏦 银行账户: ${profile.bankAccounts} 个</p><p>📞 手机号: ${profile.phones} 个</p></div>`;
    }
}
window.ComprehensiveProfileAnalyzer = ComprehensiveProfileAnalyzer;
